import 'package:flutter/cupertino.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_tv_widgets/resources/kq_theme_colors.dart';

class KqText extends StatefulWidget {
  /// 默认文本，如果设置了[controller]则此值无效
  final String data;
  final TextStyle? style;
  final StrutStyle? strutStyle;
  final TextAlign? textAlign;
  final TextDirection? textDirection;
  final Locale? locale;
  final bool? softWrap;
  final TextOverflow? overflow;
  final double? textScaleFactor;
  final int? maxLines;
  final String? semanticsLabel;
  final TextWidthBasis? textWidthBasis;
  final TextHeightBehavior? textHeightBehavior;
  final Color? selectionColor;
  final KqTextController? controller;
  final bool showBorder;
  final Color borderColor;
  final double horizontalPadding;

  /// 通过[KqTextController].text = 'str'时触发更新
  final Function(String value)? onChange;

  const KqText(
    this.data, {
    super.key,
    this.style,
    this.strutStyle,
    this.textAlign,
    this.textDirection,
    this.locale,
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
    this.semanticsLabel,
    this.textWidthBasis,
    this.textHeightBehavior,
    this.selectionColor,
    this.controller,
    this.showBorder = false,
    this.borderColor = KqThemeColors.bgBlack15,
    this.horizontalPadding = 0,
    this.onChange,
  });

  @override
  State<StatefulWidget> createState() => _KqTextState();
}

class _KqTextState extends State<KqText> {
  late KqTextController _controller;

  @override
  void initState() {
    super.initState();
    _controller = widget.controller ?? KqTextController();
    if (widget.controller == null) {
      _controller._text = widget.data;
    }
    _controller._state = this;
  }

  @override
  void didUpdateWidget(covariant KqText oldWidget) {
    _controller = widget.controller ?? KqTextController();
    if (widget.controller == null) {
      _controller._text = widget.data;
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: widget.showBorder
          ? BoxDecoration(
              border: Border.all(
                color: widget.borderColor,
                width: 1.5.r,
              ),
            )
          : null,
      padding: EdgeInsets.only(
        left: widget.horizontalPadding,
        right: widget.horizontalPadding,
      ),
      alignment: Alignment.centerLeft,
      child: Text(
        _controller.text,
        style: widget.style,
        key: widget.key,
        strutStyle: widget.strutStyle,
        textAlign: widget.textAlign,
        textDirection: widget.textDirection,
        locale: widget.locale,
        softWrap: widget.softWrap,
        overflow: widget.overflow,
        textScaleFactor: widget.textScaleFactor,
        maxLines: widget.maxLines,
        semanticsLabel: widget.semanticsLabel,
        textWidthBasis: widget.textWidthBasis,
        textHeightBehavior: widget.textHeightBehavior,
        selectionColor: widget.selectionColor,
      ),
    );
  }

  update({bool hasListener = true}) {
    setState(() {});
    if (hasListener) {
      widget.onChange?.call(_controller.text);
    }
  }
}

class KqTextController {
  _KqTextState? _state;
  String _text = '';

  String get text => _text;

  set text(String text) {
    _text = text;
    _state?.update();
  }

  /// 这个不触发回调
  set textNoListener(String text) {
    _text = text;
    _state?.update(hasListener: false);
  }
}
